/*
 * Group 3, User Interface Programming II (vt11)
 * Pooya | Flora | Monica | Can
 */
package vical.model;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import vical.controller.PreferenceController;

/**
 * This class is made for "tasks" manipulated by this calendar application.
 * Each "task" is called an "entry".
 * 
 * @author Pooya
 * @author Can
 */
public class Entry {

    private int id;
    private String name;
    private String description;
    private Calendar startTime;
    private Calendar endTime;
    private String category;
    private String priority;
    private boolean allDay;
    private String specialEvents;

    public Entry() {
        this.id = (int) System.currentTimeMillis() / 1000;
        this.name = "";
        this.description = "";
        this.startTime = Calendar.getInstance();
        this.endTime = Calendar.getInstance();
        this.endTime.add(Calendar.HOUR, 1);
        this.category = "Work";
        this.priority = "";
        this.allDay = true;
        this.specialEvents = "";
    }

    public int getId() {
        return id;
    }

    public void setId(int _id) {
        id = _id;
    }

    public String getName() {
        return name;
    }

    public void setName(String _name) {
        name = _name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String _description) {
        description = _description;
    }

    public Calendar getStartTime() {
        return startTime;
    }

    public String getStartTimeText(boolean isAllDay) {
        Locale locale = PreferenceController.getLocale();
        String timeString = "";
        timeString += startTime.getDisplayName(Calendar.MONTH, Calendar.SHORT, locale);
        timeString += " " + startTime.get(Calendar.DATE) + ",";
        timeString += " " + startTime.get(Calendar.YEAR);
        if (!isAllDay) {
            DateFormat df = DateFormat.getTimeInstance();
            timeString += " " + df.format(startTime.getTime());
        }
        return timeString;
    }

    public String getEndTimeText(boolean isAllDay) {
        Locale locale = PreferenceController.getLocale();
        String timeString = "";
        timeString += endTime.getDisplayName(Calendar.MONTH, Calendar.SHORT, locale);
        timeString += " " + endTime.get(Calendar.DATE) + ",";
        timeString += " " + endTime.get(Calendar.YEAR);
        if (!isAllDay) {
            DateFormat df = DateFormat.getTimeInstance();
            timeString += " " + df.format(endTime.getTime());
        }
        return timeString;
    }

    public void setStartTime(Calendar _startTime) {
        startTime = _startTime;
    }

    public Calendar getEndTime() {
        return endTime;
    }

    public void setEndTime(Calendar _endTime) {
        endTime = _endTime;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String _category) {
        category = _category;
    }

    public String getPriority() {
        return priority;
    }

    public void setPriority(String _priority) {
        priority = _priority;
    }

    public boolean isAllday() {
        return allDay;
    }

    public void setAllday(boolean _allDay) {
        allDay = _allDay;
    }

    public String getSpecialEvents() {
        return specialEvents;
    }

    public void setSpecialEvents(String _specialEvents) {
        specialEvents = _specialEvents;
    }

    // return true if the parameter @date is between timeString and endTime
    // by Can
    public boolean isDateContained(Calendar date) {
        Calendar startEdge = (Calendar) date.clone();
        startEdge.set(Calendar.HOUR, 0);
        startEdge.set(Calendar.MINUTE, 0);
        startEdge.set(Calendar.SECOND, 0);
        startEdge.set(Calendar.MILLISECOND, 0);
        startEdge.set(Calendar.AM_PM, Calendar.AM);
        Calendar endEdge = (Calendar) startEdge.clone();
        endEdge.roll(Calendar.DATE, true);
        if (!(this.getEndTime().before(startEdge) || this.getStartTime().after(endEdge))) {
            return true;
        }
        return false;
    }

    //Returns complete details of a Task
    public String getTaskDetailedString() {
        String str = "<html>";
        str += "Event: " + this.getName() + "<br/>";
        if (this.getDescription() != null && this.getDescription() != "") {
            str += "Description: " + this.getDescription() + "<br/>";
        }
        if (this.getCategory() != null && this.getCategory() != "") {
            str += "Category: " + this.getCategory() + "<br/>";
        }
        if (this.getPriority() != null && this.getPriority() != "") {
            str += "Priority: " + this.getPriority() + "<br/>";
        }
        if (this.getStartTime() != null && this.getEndTime() != null) {
            str += "Start Time: " + this.getStartTimeText(this.allDay) + "<br/>";
            str += "End Time: " + this.getEndTimeText(this.allDay) + "<br/>";
        }
        if (this.getSpecialEvents() != null && !this.getSpecialEvents().equals("Select...")) {
            str += "Special Events: " + this.getSpecialEvents() + "<br/>";
        }
        str += "</html>";
        return str;
    }

    @Override
    public Entry clone() {
        Entry newEntry = new Entry();
        newEntry.setId(id);
        newEntry.setName(name);
        newEntry.setDescription(description);
        newEntry.setCategory(category);
        newEntry.setAllday(allDay);
        newEntry.setPriority(priority);
        newEntry.setStartTime(startTime);
        newEntry.setEndTime(endTime);
        newEntry.setSpecialEvents(specialEvents);
        return newEntry;
    }
}
